package com.qkl.xiatang.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qkl.xiatang.app.dto.message.MessageSendVO;
import com.qkl.xiatang.app.dto.pond.PondNamesVO;
import com.qkl.xiatang.app.entity.feed.FeedAllocDetailEntity;
import com.qkl.xiatang.app.entity.feed.FeedAllocEntity;
import com.qkl.xiatang.app.mapper.FeedAllocBaseMapper;
import com.qkl.xiatang.app.utils.AppDateUtils;
import com.qkl.xiatang.app.utils.MsgUtils;
import com.qkl.xiatang.app.utils.appenum.MessageTypeEnum;
import com.qkl.xiatang.app.utils.appenum.MessageTypeSubEnum;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class FeedAllocBaseService {

    @Autowired
    private FeedAllocBaseMapper feedAllocBaseMapper;

    @Autowired
    private PondNamesService pondNamesService;


    /**
     * 是否存在配料
     * @param companyId
     * @param templateId
     * @param allocDate
     * @return
     */
    public boolean existsAllocFeed(String companyId, String templateId, Date allocDate){
        return ObjectUtils.isNotEmpty(feedAllocBaseMapper.selectList(new QueryWrapper<FeedAllocEntity>()
                                                                        .eq("company_id",companyId)
                                                                        .eq("template_id",templateId)
                                                                        .eq("alloc_date",allocDate)
                                                                        .last("limit 1")));
    }

    /**
     * 未配料消息
     * @param companyId
     * @param templateId
     * @param allocDate
     * @return
     */
    public List<MessageSendVO> queryNotAllocMessage(String companyId, String templateId, Date allocDate){

        List<FeedAllocDetailEntity> mealList = this.queryAllocFeedMeal(companyId,templateId,allocDate);
        Map<String,MessageSendVO> sendVOMap = new LinkedHashMap<>();
        if(ObjectUtils.isNotEmpty(mealList)){
            for (FeedAllocDetailEntity meal : mealList){
                PondNamesVO pondNamesVO = pondNamesService.queryNotAllocPondNames(companyId,templateId,meal.getAllocDate(),meal.getSerialNo());
                List<String> pondNames = pondNamesVO.getPondNameList();
                List<String> pondIdList = pondNamesVO.getPondIdList();
                if(ObjectUtils.isEmpty(pondNames)){
                    continue;
                }
                String pondIdKey = String.join(",",pondIdList);
                if( !sendVOMap.containsKey(pondIdKey)) {
                    MessageTypeEnum typeEnum = MessageTypeEnum.ADMIN;
                    int subType = MessageTypeSubEnum.WEIGH_FEED_NO_ALLOC.getSubType();
                    String title = MessageTypeSubEnum.WEIGH_FEED_NO_ALLOC.getName();
                    String titleSub = AppDateUtils.formatMsgDate(meal.getAllocDate()) + meal.getName();
                    String content = "塘号: " + MsgUtils.formatPondNames(pondNames);
                    MessageSendVO sendVO = MessageSendVO.create(companyId, title, titleSub, typeEnum, subType, meal.getAllocDate(), content);
                    sendVO.addDetailList(pondNames);
                    sendVO.setPondIdList(pondIdList);
                    sendVOMap.put(pondIdKey,sendVO);
                }else {
                    MessageSendVO sendVO = sendVOMap.get(pondIdKey);
                    String titleSub = sendVO.getTitleSub();
                    if(sendVO.getMsgDate().compareTo(meal.getAllocDate()) == 0){
                        titleSub = titleSub + meal.getName();
                    }else {
                        SimpleDateFormat fmt2 = new SimpleDateFormat("dd日");
                        String formatDay = fmt2.format(meal.getAllocDate());
                        if( !titleSub.contains(formatDay)){
                            titleSub = titleSub + formatDay;
                        }
                        titleSub = titleSub + meal.getName();
                    }
                    sendVO.setTitleSub(titleSub);
                }
            }
        }
        List<MessageSendVO> sendVOList = new ArrayList<>();
        sendVOList.addAll(sendVOMap.values());
        return sendVOList;
    }

    private List<FeedAllocDetailEntity> queryAllocFeedMeal(String companyId,String templateId,Date allocDate){
        return feedAllocBaseMapper.queryAllocFeedMeal(companyId,templateId,allocDate);
    }

}
